import { fileURLToPath, URL } from 'node:url'

import {ConfigEnv, defineConfig, loadEnv, UserConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import * as path from "path";


// https://vitejs.dev/config/
export default ({command, mode}: ConfigEnv):UserConfig => {
  const env = loadEnv(mode, process.cwd())
  return (
      {

        plugins: [
            createSvgIconsPlugin({
               //  指定需要缓存的图标文件夹
               iconDirs: [path.resolve(process.cwd()), 'src/assets/icons'],
               //  指定symbolId格式
               symbolId: 'icon-[dir]-[name]'
            }),
          vue(),
          vueJsx(),
        ],
        resolve: {
          alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
          }
        },
        server:{
          host: '0.0.0.0',
          port: Number(env.VITE_APP_PORT),
          proxy: {
            [env.VITE_APP_BASE_API]: {
              target: 'http://localhost:3000/v1/api',
              changeOrigin: true,
              rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
            }
          }
        }
      }
  )
}

